package com.aliyun.solutions.demo.wanxiang.utils;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * validator的校验工具
 *
 * @author solution
 * @date 2024/12/04
 */
public abstract class ValidateUtils {

    public static String buildExceptionMessage(Set<ConstraintViolation<?>> constraintViolations) {
        return constraintViolations.stream().map(p -> p.getMessage()).collect(Collectors.joining("; "));
    }

    public static String buildExceptionMessage(Exception exception) {
        if (exception instanceof ConstraintViolationException) {
            return buildExceptionMessage(((ConstraintViolationException)exception).getConstraintViolations());
        }
        return exception.getMessage();
    }
}
